package com.lonely.web.controller.order;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lonely.common.annotation.RedisLock;
import com.lonely.common.core.domain.Select;
import com.lonely.common.core.domain.StatusInfo;
import com.lonely.common.core.domain.api.R;
import com.lonely.order.domain.dto.order.*;
import com.lonely.order.domain.vo.order.OrderDetailVO;
import com.lonely.order.domain.vo.order.OrderQuantityInfo;
import com.lonely.order.domain.vo.order.OrderStatInfoVO;
import com.lonely.order.domain.vo.order.QueryProduceOrderPageVO;
import com.lonely.order.service.IOrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author: 黄志标
 * @date: 2025/6/12 17:03
 * @description: 订单管理请求类
 */
@RestController
@RequestMapping("/orderManager/order")
@Api(tags = "订单管理/订单")
public class OrderController {

    @Autowired
    private IOrderService orderService;

    /**
     * 获取订单列表
     *
     * @return
     */
    @PostMapping("/produceOrderPage")
    @ApiOperation(value = "新订单跟进分页查询")
//    @SaCheckPermission("order:followList:page")
    public R<Page<QueryProduceOrderPageVO>> produceOrderPage(@RequestBody @Validated QueryProduceOrderPageDTO queryDto) {
        return R.data(this.orderService.produceOrderPage(queryDto));
    }


    /**
     * 获取订单统计信息
     *
     * @return
     */
    @PostMapping("/getOrderStatInfo")
    @ApiOperation(value = "获取订单统计信息")
    public R<OrderStatInfoVO> getOrderStatInfo(@RequestBody @Validated QueryProduceOrderPageDTO queryDto) {
        return R.data(this.orderService.getOrderStatInfo(queryDto));
    }


    /**
     * 手动同步订单数据(最近一个月的)
     *
     * @return
     */
    @GetMapping("/synchronizerOrder")
    @ApiOperation(value = "手动同步订单数据(最近一个月的)")
    @RedisLock(key = "'order:synchronizerOrder'", waitTime = 2000)
    @SaCheckPermission("order:followList:synchronizerOrder")
    public R<Boolean> synchronizerOrder() {
        this.orderService.synchronizerLastMonthOrder();
        return R.status(true);
    }


    /**
     * 获取订单状态下拉框数据
     *
     * @return
     */
    @GetMapping("/getOrderStatusSelect")
    @ApiOperation(value = "获取订单状态下拉框数据")
    public R<List<Select>> getOrderStatusSelect() {
        return R.data(this.orderService.getOrderStatusSelect());
    }


    /**
     * 根据订单号获取订单数量统计信息
     *
     * @param orderId
     * @return
     */
    @GetMapping("/getOrderQuantityInfo/{orderId}")
    @ApiOperation(value = "根据订单号获取订单数量统计信息")
    public R<OrderQuantityInfo> getOrderQuantityInfo(@PathVariable("orderId") Long orderId) {
        return R.data(this.orderService.getOrderQuantityInfo(orderId));
    }


    /**
     * 接单
     *
     * @param receiveOrderDTO
     * @return
     */
    @PostMapping("/receiveOrders")
    @ApiOperation(value = "接单")
    @SaCheckPermission("order:followList:receiveOrders")
    public R<Boolean> receiveOrders(@RequestBody @Validated ReceiveOrderDTO receiveOrderDTO) {
        this.orderService.receiveOrders(receiveOrderDTO);
        return R.status(true);
    }


    /**
     * 打标
     *
     * @param markingOrderDTO
     * @return
     */
    @PostMapping("/markingOrders")
    @ApiOperation(value = "打标")
    @SaCheckPermission("order:followList:markingOrders")
    public R<Boolean> markingOrders(@RequestBody @Validated MarkingOrderDTO markingOrderDTO) {
        this.orderService.markingOrders(markingOrderDTO);
        return R.status(true);
    }

    /**
     * 计算工价
     *
     * @param calculateWages
     * @return
     */
    @PostMapping("/calculateWages")
    @ApiOperation(value = "计算工价")
    public R<String> calculateWages(@RequestBody @Validated CalculateWagesDTO calculateWages) {
        return R.data(this.orderService.calculateWages(calculateWages));
    }

    /**
     * 根据订单号 获取订单明细
     *
     * @param produceOrderId
     * @return
     */
    @GetMapping("/getOrderDetail/{produceOrderId}")
    @ApiOperation(value = "根据订单号获取订单明细")
    public R<OrderDetailVO> getOrderDetail(@PathVariable("produceOrderId") Long produceOrderId) {
        return R.data(this.orderService.getOrderDetail(produceOrderId));
    }

    /**
     * 订单作废
     *
     * @param invalidOrderDTO
     * @return
     */
    @PostMapping("/invalidOrders")
    @ApiOperation(value = "订单作废")
    @SaCheckPermission("order:followList:invalidOrders")
    public R<Boolean> invalidOrders(@RequestBody @Validated InvalidOrderDTO invalidOrderDTO) {
        this.orderService.invalidOrders(invalidOrderDTO);
        return R.status(true);
    }

    /**
     * 打印skc贴纸
     *
     * @param printSkcStickerDTO
     * @return
     */
    @PostMapping("/printSkcSticker")
    @SaCheckPermission("order:followList:printSkcSticker")
    public R<String> printSkcSticker(@RequestBody @Validated PrintSkcStickerDTO printSkcStickerDTO) {
        return R.data(this.orderService.printSkcSticker(printSkcStickerDTO));
    }


    /**
     * 根据订单号获取打印skc贴纸 尺码数量信息
     *
     * @param produceOrderId
     * @return
     */
    @GetMapping("/getPrintSkcStickerOrderQuantityInfo/{produceOrderId}")
    @ApiOperation(value = "根据订单号获取打印skc贴纸尺码数量信息")
    public R<OrderQuantityInfo> getPrintSkcStickerOrderQuantityInfo(@PathVariable("produceOrderId") Long produceOrderId) {
        return R.data(this.orderService.getPrintSkcStickerOrderQuantityInfo(produceOrderId));
    }

    /**
     * 获取状态侧边栏信息
     *
     * @param queryDto
     * @return
     */
    @PostMapping("/getStatusInfo")
    public R<List<StatusInfo>> getStatusInfo(@RequestBody @Validated QueryProduceOrderPageDTO queryDto) {
        return R.data(this.orderService.getStatusInfo(queryDto));
    }

    /**
     * 国内大货下单
     *
     * @param produceOrderId
     * @return
     */
    @GetMapping("/domesticBulkGoodsPlaceOrder/{produceOrderId}")
    public R<Boolean> domesticBulkGoodsPlaceOrder(@PathVariable("produceOrderId") Long produceOrderId) {
        this.orderService.domesticBulkGoodsPlaceOrder(produceOrderId);
        return R.status(true);
    }

    /**
     * 验证生成计划单的参数
     *
     * @param generatePlanDTO
     * @return
     */
    @PostMapping("/validateGeneratePlan")
    public R<String> validateGeneratePlan(@RequestBody @Validated GeneratePlanDTO generatePlanDTO) {
        return R.data(this.orderService.validateGeneratePlan(generatePlanDTO));
    }


    /**
     * 生成计划单
     *
     * @param generatePlanDTO
     * @return
     */
    @PostMapping("/generatePlan")
    public R<Boolean> generatePlan(@RequestBody @Validated GeneratePlanDTO generatePlanDTO) {
        this.orderService.generatePlan(generatePlanDTO);
        return R.status(true);
    }
}
